package com.lzxuni.modules.system;

import com.alibaba.fastjson.JSON;
import com.lzxuni.common.exception.LzxException;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.system.entity.Editor;
import com.lzxuni.modules.system.service.EditorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨国营
 * @since 2019-06-27
 */
@RestController
@RequestMapping("/LR_SystemModule/Ueditor")
public class EditorController extends BaseController {
	@Autowired
	private EditorService editorService;

	// 列表页
	@RequestMapping("/Index")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("/LR_SystemModule/Ueditor/index");
		return mv;
	}

	// 列表
	@RequestMapping("/GetPageList")
	public Object GetPageList(String pagination, Editor editor) throws LzxException {
		PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
		PageData pageData = getPageData(editorService.queryPage(pageParameter, editor));

		return R.ok().put("data",pageData);
	}

	// 添加修改展示页面
	@RequestMapping("/Form")
	public ModelAndView insert(){
		ModelAndView mv = new ModelAndView("/LR_SystemModule/Ueditor/form");
		return mv;
	}
	@RequestMapping("GetEntity")
	public Object  GetEntity(String keyValue) {
		Editor editor = editorService.getById(keyValue);
		return R.ok().put("data", editor);
	}
	//添加或者修改操作
	@RequestMapping("/SaveForm")
	public Object insertDo(String keyValue, Editor editor) {
		editor.setId(keyValue);
		if(StringUtils.isEmpty(editor.getId())){
			editorService.save(editor);
			return R.ok("保存成功");
		}else{
			editorService.updateById(editor);
			return R.ok("修改成功");
		}
	}

	//删除
	@RequestMapping("/DeleteForm")
	public Object delete(String keyValue) {
		editorService.removeById(keyValue);
		return R.ok("删除成功");
	}

}

